package com.util;

import java.util.HashMap;
import java.util.Map;

import org.json.JSONArray;
import org.json.JSONObject;


public class GaodeUtil {
	private static GaodeUtil gu;
	public static GaodeUtil getIns()
	{
		if(gu==null)gu=new GaodeUtil();
		return gu;
	}
	public static String key="bccbeab83c04aa97bdb1cdd14680e933";//应用的KEY
	public static String sid="127351";//服务ID
	/**
	 * 创建一个服务
	 */
	public void creatServer()
	{
		Map<String,String> reqmap=new HashMap<String, String>();
		reqmap.put("key",key );
		reqmap.put("name", "顺溜儿代价司机端轨迹服务");
		reqmap.put("desc", "记录顺溜儿代价司机端猎鹰服务轨迹");
		
		
		String res=HttpKit.post("https://tsapi.amap.com/v1/track/service/add", reqmap, "");
		System.out.println(res);
	}
	/**
	 * 根据用户ID创建终端
	 */
	public void creatUser(String userid)
	{
		Map<String,String> reqmap=new HashMap<String, String>();
		reqmap.put("key",key );
		reqmap.put("sid",sid );
		reqmap.put("name", userid);
		
		String res=HttpKit.post("https://tsapi.amap.com/v1/track/terminal/add", reqmap, "");
		System.out.println(res);
		
	}
	/**
	 * 查询终端
	 */
	public void getUser()
	{
		Map<String,String> reqmap=new HashMap<String, String>();
		reqmap.put("key",key );
		reqmap.put("sid",sid );
		
		String res=HttpKit.post("https://tsapi.amap.com/v1/track/terminal/list", reqmap, "");
		System.out.println(res);
	}
	/**
	 * 创建一条轨迹
	 */
	public void creatTrid()
	{
		Map<String,String> reqmap=new HashMap<String, String>();
		reqmap.put("key",key );
		reqmap.put("sid",sid );
		reqmap.put("tid","247079376" );
		
		String res=HttpKit.post("https://tsapi.amap.com/v1/track/trace/add", reqmap, "");
		System.out.println(res);
	}
	/**
	 * 查询轨迹信息
	 * @param tid
	 * @param trid
	 */
	public void getTrid(String tid,String trid)
	{
		Map<String,String> reqmap=new HashMap<String, String>();
		reqmap.put("key",key );
		reqmap.put("sid",sid );
		reqmap.put("tid",tid);
		reqmap.put("trid",trid );
		 
		String res=HttpKit.post("https://tsapi.amap.com/v1/track/terminal/trsearch", reqmap, "");
		System.out.println(res);
		
	}
	/**
	 * 查询行政区域
	 * @param city
	 */
	public JSONObject getCity(String city)
	{
		JSONObject data=new JSONObject();
		JSONObject res=new JSONObject(HttpKit.get("https://restapi.amap.com/v3/config/district?key="+key+"&keywords="+city+"&subdistrict=0"));
		if(res!=null&&res.getString("info").equals("OK"))
		{
//			System.out.println(res.toString());
			JSONObject cityJson= (JSONObject) res.getJSONArray("districts").get(0);
			data.put("name",cityJson.getString("name"));
			data.put("center",cityJson.getString("center"));
			data.put("code","00");
		}else{
			data.put("code","01");
		}
		System.out.println(data.toString());
		return data;
	}
	
	/**
	 * 查询本KEY下的所有服务
	 */
	public void getServise()
	{
		String res=HttpKit.get("https://tsapi.amap.com/v1/track/service/list?key="+key);
		System.out.println(res);
	}
	
	/**
	 * 根据坐标解析地址，批量
	 * @param arr String格式：经度，维度
	 */
	public static JSONObject regeo(String[] arr)
	{
		StringBuffer sb=new StringBuffer();
		for(int i=0;i<arr.length;i++)
		{
			if(i!=0)sb.append("|");
			sb.append(arr[i]);
		}
		String location=sb.toString();
		System.out.println("lo:"+location);
		String res=HttpKit.get("https://restapi.amap.com/v3/geocode/regeo?key="+key+"&location="+location+"&batch=true");
		System.out.println(res);
		return new JSONObject(res);
	}
	
	
	
	/**
	 * 根据坐标解析地址，批量
	 * @param arr String格式：经度，维度
	 */
	public static String[][] regeo(String[][] arr)
	{
		String[][] ret=new String[arr.length][2];
		StringBuffer sb=new StringBuffer();
		for(int i=0;i<arr.length;i++)
		{
			if(i!=0)sb.append("|");
			sb.append(arr[i][0]+"|"+arr[i][1]);
		}
		String location=sb.toString();
		String res=HttpKit.get("https://restapi.amap.com/v3/geocode/regeo?key="+key+"&location="+location+"&batch=true");
		System.out.println(res);
		JSONObject data=new JSONObject(res);
		if(data!=null&&data.getString("status").equals("1"))
		{
			JSONArray jsa=data.getJSONArray("regeocodes");
			for(int j=0;j<(jsa.length()/2);j++)//因为入参是二位数组，所以长度一定是偶数
			{
				JSONObject datas=(JSONObject)jsa.get(j*2);
				JSONObject datae=(JSONObject)jsa.get(j*2+1);
				ret[j][0]=StrKit.isBlank(datas.getString("formatted_address"))?"不详":datas.getString("formatted_address");
				ret[j][1]=StrKit.isBlank(datae.getString("formatted_address"))?"不详":datae.getString("formatted_address");
			}
		}
		
		return ret;
	}
	public static void main(String[] args) {
//		String[] arr={"126.653794,45.727096","126.657399,45.727245"};
//		regeo(arr);
		
		
		
		
//		getIns().creatServer();
//		GaodeUtil.getIns().getServise();
//		getIns().creatUser("20191223111356313551");
//		getIns().getUser();
//		getIns().creatTrid();
//		getIns().getTrid("247079376", "20");
//		getIns().getCity("沈阳市");
	}
}
